/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.zjx8969.myapplication;

import android.content.Context;
import android.text.TextUtils;

import java.util.Locale;

public class LanguageUtil {
    private static String st_msCountry = "";
    private static String net_sCountry = "";
    private static int st_nSysLanguage = LanguageUtil.LAN_ENGLISH;

    //系统语言相关
    public static final int LAN_OTHER = -1;//默认情况，无法获取语言代码或者非以下12种语言支持范围之内
    public static final int LAN_CHINESE = 0;
    public static final int LAN_ENGLISH = 1;
    public static final int LAN_JAPANESE = 2;
    public static final int LAN_KOREAN = 3;
    // public static final int Arabic = 4;
    // public static final int Hindi = 5;
    public static final int LAN_BRAZIL = 6;
    public static final int LAN_GERMAN = 7;
    public static final int LAN_ITALIAN = 8;
    public static final int LAN_RUSSIAN = 9;
    public static final int LAN_SPANISH = 10;
    public static final int LAN_FRENCH = 11;
    public static final int LAN_TAIWAN = 12;
    public static final int LAN_INDONESIA = 13;

    //国家地区代码
    public final static String USA = "US";//美国
    public final static String CHINA = "CN";//中国
    public final static String BRAZIL = "BR";//巴西
    public final static String CANADA = "CA";//加拿大
    public final static String SPAIN = "ES";//西班牙
    public final static String INDONESIA = "ID";//印尼
    public final static String INDIA = "IN";//印度
    public final static String TAIWAN = "TW";//台湾
    public final static String ITALY = "IT";//意大利
    public final static String JAPAN = "JP";//日本
    public final static String KOREA = "KR";//韩国
    public final static String RUSSIA = "RU";//俄罗斯
    public final static String FRANCE = "FR";//法国
    public final static String GERMANY = "DE";//德国
    public final static String MEXICO = "MX";//墨西哥
    public final static String NEWZEALAND = "NZ";//新西兰
    public final static String AUSTRALIA = "AU";//澳大利亚
    public final static String UNITEDKINDOM = "GB";//英国
    public final static String THAILAND = "TH";

    public static void checkSetCurLanguage() {
        Locale locale = Locale.getDefault();
        String language = locale.getLanguage().toLowerCase(Locale.US);
        if (language != null) {
            if (language.compareTo("zh") == 0) {
                if (locale.toString().startsWith("zh_CN")) {
                    st_nSysLanguage = LAN_CHINESE;
                } else if (locale.toString().startsWith("zh_TW")) {
                    st_nSysLanguage = LAN_TAIWAN;// Locale.TRADITIONAL_CHINESE
                } else {
                    st_nSysLanguage = LAN_ENGLISH;//zh_HK,zh_SG,etc
                }
            } else if (language.compareTo("en") == 0) {
                st_nSysLanguage = LAN_ENGLISH;
            } else if (language.compareTo("ja") == 0) {
                st_nSysLanguage = LAN_JAPANESE;
            } else if (language.compareTo("ko") == 0) {
                st_nSysLanguage = LAN_KOREAN;
                // } else if (language.compareTo("ar") == 0) {
                // st_nSysLanguage = MakeupApp.Arabic;
                // } else if (language.compareTo("hi") == 0) {
                // st_nSysLanguage = MakeupApp.Hindi;
            } else if (language.compareTo("pt") == 0) {
                st_nSysLanguage = LAN_BRAZIL;
            } else if (language.compareTo("de") == 0) {
                st_nSysLanguage = LAN_GERMAN;
            } else if (language.compareTo("it") == 0) {
                st_nSysLanguage = LAN_ITALIAN;
            } else if (language.compareTo("ru") == 0) {
                st_nSysLanguage = LAN_RUSSIAN;
            } else if (language.compareTo("es") == 0) {
                st_nSysLanguage = LAN_SPANISH;
            } else if (language.compareTo("fr") == 0) {
                st_nSysLanguage = LAN_FRENCH;
            } else if (language.compareTo("in") == 0) {
                st_nSysLanguage = LAN_INDONESIA;
            } else
                st_nSysLanguage = LAN_ENGLISH;
        }
    }

    public static void checkSetCurCountry(Context context) {
//        //因为设置了从网络注册获取国家代码，所以测试手机无法定位到US，现在开放接口供QA修改
//        if (BuildConfig.DEBUG) {
//            String country = PreferenceUtil.getString(context, DevicePref.FILE_DEVICE_CONFIG,
//                    DevicePref.KEY_LAST_COUNTRY, "");
//            if (!TextUtils.isEmpty(country)) {
//                st_msCountry = country;
//                return;
//            }
//        }
//        String lastCountry = LanguageUtil.getCountryCode();
//        if (TextUtils.isEmpty(lastCountry)) {
//            st_msCountry = LanguageUtil.getCountryCode();
//            LogUtil.logE("CountryCode", "从本地设置获取 = " + st_msCountry);
//        } else {
//            st_msCountry = lastCountry;
//            LogUtil.logE("CountryCode", "从网络运营商获取 = " + st_msCountry);
//        }

        st_msCountry = LanguageUtil.getCountryCode();
    }

    public static void checkNetWorkCountry(Context context) {
        String country = CountryUtil.getCountryFromNetwork(context);
        if (TextUtils.isEmpty(country)) {
            net_sCountry = LanguageUtil.getCountryCode();
        } else {
            net_sCountry = country;
        }
    }

    public static int curSysLanguage() {
        return st_nSysLanguage;
    }

    public static String curSysCountry() {
        return st_msCountry;
    }

    public static String curNetCountry() {
        return net_sCountry;
    }

    //cl2567,2018,7,5统一调用接口
    public static boolean curCountryIs(String country) {
        return isWhere(country);
    }

    /**
     * 判断当前所处国家，具体请百度国家地区代码
     * <p>isUSA = LanguageUtil.isWhere(this, LanguageUtil.USA);</p>
     *
     * @param country 国家地区代码，如“US”“CA”
     * @return boolean
     */
    public static boolean isWhere(String country) {
        String localCountry = st_msCountry;
        if (TextUtils.isEmpty(localCountry)) {
            localCountry = UNITEDKINDOM;
        }
        if (localCountry.equalsIgnoreCase(country)) {
            return true;
        }
        return false;
    }

    //获取国家地区代码
    public static String getCountryCode() {
        String localCountry = CountryUtil.getCountryFromLocale();
        if (TextUtils.isEmpty(localCountry)) {
            localCountry = UNITEDKINDOM;
        } else {
            localCountry = localCountry.toLowerCase(Locale.US);
        }
        return localCountry;
    }

    public static String getLanguageCode() {
        Locale locale = Locale.getDefault();
        return locale.getLanguage().toLowerCase(Locale.US);
    }

//    public static boolean isAbricLanuage(Context context){
//        String language  = context.getResources().getConfiguration().locale.getLanguage();
//        if(!TextUtils.isEmpty(language) && language.endsWith("ar")){
//            return true;
//        }
//        return false;
//    }

    public static boolean compareLanguage(int languageCode) {
        return languageCode == curSysLanguage();
    }

    /**
     * 根据语言获取区域标志，用于组装不同国家的请求URL
     *
     * @return country code
     */
    public static String getLocaleLanguage() {
        String lang;
        switch (curSysLanguage()) {
            case LAN_ENGLISH:
                lang = "en";
                break;
            case LAN_JAPANESE:
                lang = "jp";
                break;
            case LAN_CHINESE:
                lang = "cs";
                break;
            case LAN_KOREAN:
                lang = "kr";
                break;
            case LAN_FRENCH:
                lang = "fr";
                break;
            case LAN_GERMAN:
                lang = "de";
                break;
            case LAN_ITALIAN:
                lang = "it";
                break;
            case LAN_SPANISH:
                lang = "es";
                break;
            case LAN_BRAZIL:
                lang = "pt";
                break;
            case LAN_RUSSIAN:
                lang = "ru";
                break;
            case LAN_TAIWAN:
                lang = "ct";
                break;
            case LAN_INDONESIA:
                lang = "id";
                break;
            default:
                lang = "en";
                break;
        }
        return lang;
    }


    private static String[] erCountries = new String[]{"AT", SPAIN, ITALY, FRANCE, GERMANY, UNITEDKINDOM, "BE"
            , "BG", "CY", "CZ", "DK", "EE", "FI", "GR", "HU", "IE", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK",
            "SI", "SE", "HR", "IS", "NO", "GF", "MQ", "LI", "GP", "YT", "RE", "MF","SH","AC","TA"
            ,"BV","TF","GS","IO","AD","FO","GI","GG","VA","IM","JE","MC","SM","SJ","CH"
            ,"AI","AW","BM","VG","KY","GL","MS","BL","PM","SX","TC","PF","NC","PN","WF","CW","FK"
            ,"AX","BQ"};

    public static boolean isEUcountry() {

        for (String country : erCountries) {
            if (country.equalsIgnoreCase(net_sCountry)) {
                return true;
            }
        }

        return false;

    }
}
